/*
 * AuthenticateWS.java
 * 
 * Created on 19/11/2007, 20:51:55
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ita.redecasd.mi.ws;

import br.ita.redecasd.mi.auth.SignValidator;
import br.ita.redecasd.mi.db.Session;
import br.ita.redecasd.mi.ldap.LDAPManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.naming.NamingException;

/**
 *
 * @author mi
 */
@WebService()
public class AuthenticateWS {

/**
     * Web service operation
     */
    @WebMethod(operationName = "getPatientInfo")
    public String[] getPatientInfo(@WebParam(name = "login")
    String login)  {
        try
        {
            LDAPManager ldap = new LDAPManager();
            return ldap.getPatientDetails(login);
        }
        catch( NamingException ne )
        {}
        
        return null;
        
    }

/**
     * Web service operation
     */
    @WebMethod(operationName = "validateSign")
    public String validateSign(@WebParam(name = "userName")
    String userName, @WebParam(name = "userAgent")
    String userAgent, @WebParam(name = "signature")
    byte[] signature)  {
        try {
            boolean validated = SignValidator.validateSignature(userName, userAgent, signature);
            if (validated) {
                try {
                    String sessionID = userName + userAgent;
                    Session.getInstance().createSession(sessionID);
                    return sessionID;
                } catch (Exception sqle) {
                }
            }

            
        } catch (NamingException ex) {
            Logger.getLogger(AuthenticateWS.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch( Exception e )
        {
            Logger.getLogger(AuthenticateWS.class.getName()).log(Level.SEVERE, null, e );
        }
        
        return null;
    }

/**
     * Web service operation
     */
    @WebMethod(operationName = "mustAuthenticate")
    public boolean mustAuthenticate(@WebParam(name = "medLogin")
    String medLogin, @WebParam(name = "patientLogin")
    String patientLogin)  {
        try {
            return Session.getInstance().physistCanGetPatientDetails(medLogin, patientLogin);
	} catch (ClassNotFoundException ex) {
            Logger.getLogger(AuthenticateWS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            // TODO Bloco catch gerado automaticamente
            e.printStackTrace();
	}
        return true;
	
    }

/**
     * Web service operation
     */
    @WebMethod(operationName = "validateSession")
    public boolean validateSession(@WebParam(name = "userAgent")
    String userAgent, @WebParam(name = "username")
    String username) {
        try
        {
            return Session.getInstance().validateSession(userAgent);

        }
        catch( Exception e )
        {
            return false;
        }
    }

/**
     * Web service operation
     */
    @WebMethod(operationName = "updateACL")
    public void updateACL(@WebParam(name = "medLogin")
    String medLogin, @WebParam(name = "patientLogin")
    String patientLogin)  {
        try {
            Session.getInstance().giveMedAccessToPatientsRecords(medLogin, patientLogin );
	} catch (ClassNotFoundException ex) {
            Logger.getLogger(AuthenticateWS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            // TODO Bloco catch gerado automaticamente
            e.printStackTrace();
	}
    }

}
